{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 15.4 Longest common subsequence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.4-1\n",
    "\n",
    "> Determine an LCS of $\\langle 1, 0, 0, 1, 0, 1, 0, 1\\rangle$ and $\\langle 0, 1, 0, 1, 1, 0, 1, 1, 0\\rangle$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\langle 1, 0, 0, 1, 1, 0 \\rangle$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.4-2\n",
    "\n",
    "> Give pseudocode to reconstruct an LCS from the completed $c$ table and the original sequences $X = \\langle x_1, x_2, \\dots, x_m \\rangle$ and $Y = \\langle y_1, y_2, \\dots, y_n\\rangle$ in $O(m + n)$ time, without using the $b$ table."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "PRINT-LCS(c, X, Y, i, j)\n",
    " 1 if c[i][j] == 0\n",
    " 2     return\n",
    " 3 if X[i] == Y[j]\n",
    " 4     PRINT-LCS(c, X, Y, i - 1, j - 1)\n",
    " 5     print X[i]\n",
    " 6 elseif c[i - 1][j] > c[i][j - 1]\n",
    " 7     PRINT-LCS(c, X, Y, i - 1, j)\n",
    " 8 else\n",
    " 9     PRINT-LCS(c, X, Y, i, j - 1)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.4-3\n",
    "\n",
    "> Give a memoized version of LCS-LENGTH that runs in $O(mn)$ time."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "LCS-LENGTH(X, Y, i, j)\n",
    " 1 if c[i, j] > -1\n",
    " 2     return c[i, j]\n",
    " 3 if i == 0 or j == 0\n",
    " 4     return c[i, j] = 0\n",
    " 5 if xi = yj\n",
    " 6     return c[i, j] = LCS-LENGTH(X, Y, i - 1, j - 1) + 1\n",
    " 7 return c[i, j] = max(LCS-LENGTH(X, Y, i - 1, j), LCS-LENGTH(X, Y, i, j - 1))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.4-4\n",
    "\n",
    "> Show how to compute the length of an LCS using only $2 \\cdot \\min(m, n)$ entries in the $c$ table plus $O(1)$ additional space. Then show how to do the same thing, but using $\\min(m, n)$ entries plus $O(1)$ additional space."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$2 \\cdot \\min(m, n)$: rolling.\n",
    "\n",
    "$\\min(m, n)$: save the old value of the last computed position."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.4-5\n",
    "\n",
    "> Give an $O(n^2)$-time algorithm to find the longest monotonically increasing subsequence of a sequence of $n$ numbers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the LCS of the original sequence and the sorted sequence, $O(n \\lg n) + O(n^2)=O(n^2)$ time."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.4-6 $\\star$\n",
    "\n",
    "> Give an $O(n \\lg n)$-time algorithm to find the longest monotonically increasing subsequence of a sequence of $n$ numbers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Binary search."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
